{*******************************************************************************
* The contents of this file are used with permission, subject to the Mozilla   *
* Public License Version 1.1 (the "License"); you may not use this file except *
* in compliance with the License. You may obtain a copy of the License at      *
* http://www.mozilla.org/MPL/                                                  *
*                                                                              *
* Software distributed under the License is distributed on an "AS IS" basis,   *
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for *
* the specific language governing rights and limitations under the License.    *
*                                                                              *
* The Original Code is distributed as part of the "mediate" product and is     *
* Copyright (C) @home media limited. All Rights Reserved.                      *
*                                                                              *
* Email: support@athomemedia.co.uk                                             *
* Web:   http://www.athomemedia.co.uk                                          *
*******************************************************************************}

unit Viewport;

interface

uses Graphics, Types, Colours, Graphic, Canvas, AHMTypes;

type
  TAHMViewport = class
  private
    FOwner: TObject;
    FClipping: Boolean;
    FWidth: Single;
    FHeight: Single;
    FScreenWidth: Single;
    FScreenHeight: Single;
    FXOrigin: Single;
    FYOrigin: Single;
    FDrawingCanvas: TAHMCanvas;
  protected
    function GetAbsoluteRect: TAHMRectF;
    function GetVirtualXOrigin: Integer;
    function GetVirtualYOrigin: Integer;
  public
    constructor Create;
    property Owner: TObject read FOwner write FOwner;
    property Clipping: Boolean read FClipping write FClipping;
    property Canvas: TAHMCanvas read FDrawingCanvas;
    property Width: Single read FWidth write FWidth;
    property Height: Single read FHeight write FHeight;
    property ScreenWidth: Single read FScreenWidth;
    property ScreenHeight: Single read FScreenHeight;
    property XOrigin: Single read FXOrigin write FXOrigin;
    property YOrigin: Single read FYOrigin write FYOrigin;
    property VirtualXOrigin: Integer read GetVirtualXOrigin;
    property VirtualYOrigin: Integer read GetVirtualYOrigin;
    function ClientRect(X, Y, Width, Height: Single): TAHMRectF;
    function ClippedClientRect(X, Y, Width, Height: Single): TAHMRectF;
    function ViewportRect: TAHMRectF;
  end;

implementation

uses Screen;

constructor TAHMViewport.Create;
begin
  inherited Create;

  // Populate properties from main Screen instance
  FScreenWidth := ScreenInstance.ScreenRect.Width;
  FScreenHeight := ScreenInstance.ScreenRect.Height;
  FDrawingCanvas := ScreenInstance.DrawingCanvas;
end;

function TAHMViewport.GetAbsoluteRect: TAHMRectF;
begin
  // Calculate absolute position on screen
  Result := MakeRectF(FXOrigin, FYOrigin, FWidth, FHeight);
end;

function TAHMViewport.GetVirtualXOrigin: Integer;
begin
  // Scale origin to screen virtual coordinates
  Result := ScreenInstance.ScalePhysicalX(FXOrigin);
end;

function TAHMViewport.GetVirtualYOrigin: Integer;
begin
  // Scale origin to screen virtual coordinates
  Result := ScreenInstance.ScalePhysicalY(FYOrigin);
end;

function TAHMViewport.ClientRect(X, Y, Width, Height: Single): TAHMRectF;
begin
  Result := MakeRectF(FXOrigin + X, FYOrigin + Y, Width, Height);
end;

function TAHMViewport.ClippedClientRect(X, Y, Width, Height: Single): TAHMRectF;
begin
  // Get unclipped canvas rectangle first
  Result := ClientRect(X, Y, Width, Height);

  // Clip canvas to screen boundaries
  ClipRect(Result, ScreenInstance.ScreenRect);

  // Clip to viewport boundaries if clipping enabled
  if FClipping then
    ClipRect(Result, GetAbsoluteRect);
end;

function TAHMViewport.ViewportRect: TAHMRectF;
begin
  Result := ClientRect(0, 0, FWidth, FHeight);
end;


end.
